//
//  ChatContentView.m
//  JiaoX
//
//  Created by yinqy on 15/3/17.
//  Copyright (c) 2015年 北京娇羞科技有限公司. All rights reserved.
//

#import "ChatContentView.h"
#import "EmotionUtil.h"
#import "ChatLabel.h"
#import "UIImageView+WebCache.h"

@interface ChatContentView()
{
    UIImageView *_imageView;
    ChatLabel *_label;
    UIImageView *_preImageView;
}

@end
@implementation ChatContentView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _imageView =  [[UIImageView alloc]init];
        [self addSubview:_imageView];
        
        _label = [[ChatLabel alloc]init];
        [self insertSubview:_label aboveSubview:_imageView];
        
        _preImageView = [[UIImageView alloc]init];
        [self insertSubview:_preImageView aboveSubview:_imageView];
        
    }
    return self;
}

-(void)setText:(NSString *)text resType:(MessageResType)type{
    [_label setHidden:NO];
    [_preImageView setHidden:YES];
    [_imageView setContentMode:UIViewContentModeScaleToFill];
    [_imageView setUserInteractionEnabled:NO];
    if (type==MessageResTypeTo) {
        //代表发出去的
        [_imageView setImage:[UIImage resizedImage:@"bg_chat_to"]];
        [_label setX:10];
    }else{
        //代表收到的消息
        [_imageView setImage:[UIImage resizedImage:@"bg_chat_from"]];
        [_label setX:15];
    }
    [_label setAttributeString:text];
    [_label setNeedsDisplay];
}

-(void)setImage:(JXMessage *)msg resType:(MessageResType)type;{
    [_label setHidden:YES];
    [_preImageView setHidden:NO];
    [_imageView setContentMode:UIViewContentModeScaleAspectFit];
    if (type==MessageResTypeTo) {
        //发出去的
        [_preImageView setImage:[UIImage resizedImage:@"pre_img_to"]];
    }else{
        //收到的消息
        [_preImageView setImage:[UIImage resizedImage:@"pre_img_from"]];
    }
    if ([msg.actualContent hasPrefix:@"http:"]) {
//        [_imageView sd_setImageWithURL:[NSURL URLWithString:msg.actualContent]];
        [_imageView sd_setImageWithURL:[NSURL URLWithString:msg.actualContent] placeholderImage:[UIImage imageNamed:@"bg_placeholder"] options:0 progress:nil completed:nil];
    }else{
        NSString *file = [NSString pathDocumentsWithFileName:msg.messageId];
        [_imageView setImage:[UIImage imageWithContentsOfFile:file]];
    }
    _imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *singleTap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showImage:)];
    [_imageView addGestureRecognizer:singleTap1];
}

-(void)setFrame:(CGRect)frame{
    [super setFrame:frame];
    [_imageView setFrame:(CGRect){{0,0},frame.size}];
    [_preImageView setFrame:_imageView.frame];
    [_label setHeight:frame.size.height];
    [_label setWidth:frame.size.width-25];
}

-(void)showImage:(UITapGestureRecognizer *)ges{
    [self showImageView:((UIImageView *)ges.view)];
}


-(void)showImageView:(UIImageView *)avatarImageView{
    UIImage *image=avatarImageView.image;
    UIWindow *window=[UIApplication sharedApplication].keyWindow;
    UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    self.oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:window];
    backgroundView.backgroundColor=[UIColor blackColor];
    backgroundView.alpha=0;
    UIImageView *imageView=[[UIImageView alloc]initWithFrame:self.oldframe];
    imageView.image=image;
    imageView.tag=1;
    [backgroundView addSubview:imageView];
    [window addSubview:backgroundView];
    
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
    [backgroundView addGestureRecognizer: tap];
    
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
        backgroundView.alpha=1;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView=tap.view;
    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=self.oldframe;
        backgroundView.alpha=0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
}

@end
